// Created by WXX on 2021/12/7 19:05
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<vector<int>> g;
    vector<vector<bool>> st, border;
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    int n, m;

    vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int c) {

        g = grid;
        n = g.size(), m = g[0].size();
        st = vector<vector<bool>>(n, vector<bool>(m, false));
        border = vector<vector<bool>>(n, vector<bool>(m, false));
        dfs(row, col, grid[row][col]);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (border[i][j])
                    g[i][j] = c;
        return g;
    }

    void dfs(int x, int y, int val) {

        st[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m || g[a][b] != val) {
                border[x][y] = true;
                continue;
            }
            if (st[a][b]) continue;
            dfs(a, b, val);
        }
    }
};

void OutputBasicArray2D(vector<vector<int>> nums) {

    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        if (i > 0 && i < nums.size()) cout << ' ';
        cout << "[";
        for (int j = 0; j < nums[i].size(); j++) {
            cout << nums[i][j];
            if (j != nums[i].size() - 1) cout << ", ";
        }
        cout << "]";
        if (i != nums.size() - 1) cout << ", " << endl;
    }
    cout << "]" << endl;
}

int main() {

    vector<vector<int>> grid = {
            {1, 1},
            {1, 2}
    };
    OutputBasicArray2D(Solution().colorBorder(grid, 0, 0, 3));

    return 0;
}
